<?php
namespace app\admin\controller;

use app\admin\service\AlbumService;

class Album extends CommonController
{
    /**
     * @desc
     * @return \think\response\View
     */
    public function index()
    {
        $gid = $this->request['gid'] ?? 0;
        $aid = $this->admin_id;
        $groups = AlbumService::getGroupList($aid);
        $count = AlbumService::getAlbumCount($aid, $gid);

        return view('album/index', [
            'count' => $count,
            'groups' => $groups,
            'gid' => $gid]);
    }

    /**
     * @desc 获取图片分页数据
     * @return \think\response\Json
     */
    public function grid()
    {
        if ($this->isAjax())
        {
            $request = $this->request;
            $aid = $this->admin_id;
            $gid = $request['gid'] ?? 0;
            $data = AlbumService::getPageList($aid, $gid);
            return $this->success($data);
        }
    }

    /**
     * @desc 修改图片名称
     */
    public function updatealbum()
    {
        $request = $this->request;
        $ids = $request['ids'];
        $data = [];
        if (!empty($request['name']))
        {
            $data['name'] = $request['name'];
        }
        if (!empty($request['gid']))
        {
            $data['groupid'] = $request['gid'];
        }
        AlbumService::updateAlbum($ids, $data);
        return $this->success();
    }

    /**
     * @desc 删除图片
     * @return \think\response\Json
     */
    public function delalbum()
    {
        $request = $this->request;
        $ids = $request['ids'];
        AlbumService::delAlbum($ids);
        return $this->success();
    }

    /**
     * @desc 添加分组
     * @return \think\response\Json
     */
    public function addgroup()
    {
        $request = $this->request;
        $name = $request['name'];
        AlbumService::addGroup($name);
        return $this->success();
    }

    /**
     * @desc 编辑分组名称
     * @return \think\response\Json
     */
    public function editgroup()
    {
        $request = $this->request;
        $gid = $request['gid'];
        $name = $request['name'];
        AlbumService::editGroup($gid, $name);
        return $this->success();
    }

    /**
     * @desc 删除分组
     * @return \think\response\Json
     */
    public function delgroup()
    {
        $request = $this->request;
        $ids = $request['ids'];
        AlbumService::delGroup($ids);
        return $this->success();
    }
}
